package com.zhoutao.blog.seven.api;

import com.zhoutao.blog.seven.model.AdminTableResp;
import com.zhoutao.blog.seven.model.PageList;
import com.zhoutao.blog.seven.model.Response;
import com.zhoutao.blog.seven.model.detail.ArticleDetail;
import com.zhoutao.blog.seven.model.request.ArticleCreateRequest;
import com.zhoutao.blog.seven.model.response.Article;
import com.zhoutao.blog.seven.service.ArticleService;
import com.zhoutao.blog.seven.service.query.ArticleQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/api/articles")
public class AdminArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping
    public AdminTableResp<ArticleDetail> articleList(
            @RequestParam(defaultValue = "1", required = false) Integer page,
            @RequestParam(defaultValue = "10", required = false) Integer limit) {
        PageList<ArticleDetail> articlePageList = articleService.find(ArticleQuery.EMPTY, page, limit);
        return AdminTableResp.success(articlePageList);
    }


    @GetMapping("/{id}")
    public Response<ArticleDetail> articleList(@PathVariable Long id) {
        return Response.success(articleService.findById(id));
    }

    @PostMapping
    public Response<Long> update(@RequestBody ArticleCreateRequest request) {
        Article article = new Article();
        article.setId(request.getId())
                .setCategoryId(request.getCategoryId())
                .setMkContent(request.getMkContent())
                .setContent(request.getHtmlContent())
                .setAvatar(request.getCoverUrl())
                .setTitle(request.getTitle())
                .setDescription(request.getDesc())
                .setKeys(request.getTags())
                .setType(request.getType())
                .setIcon(request.getIcon());
        Long id = articleService.save(article);
        return Response.success(id);
    }

}
